package com.sskj.hhhjys;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.hhhjys.bean.NewStock;
import com.sskj.hhhjys.http.HttpConfig;
import com.sskj.hhhjys.presenter.BasePresenter;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.http.JsonCallBack;

import java.util.List;

import io.reactivex.disposables.Disposable;


/**
 * @author Hey
 * Create at  2019/05/10
 */
class MarketPresenter extends BasePresenter<MarketFragment> {

    public void getData() {
        OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.MARKET_LIST)
                .execute(new JsonCallBack<HttpData<List<CoinBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<CoinBean>>> response) {
                        HttpData<List<CoinBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            if (httpData.getData() != null && httpData.getData().size() > 0) {
                                mView.setData(httpData.getData());
                            }
                        }
                    }
                });

    }

    private WebSocketObserver stockSocket;

    private Disposable stockDisposable;

    public void initWebSocket() {
        if (stockSocket == null) {
            stockSocket = httpService.pushCoinType();
            stockSocket
                    .map(s -> new Gson().fromJson(s, CoinBean.class))
                    .subscribe(coinBean -> RxBus.getDefault().post(coinBean), e -> {
                    });

        } else {
//            stockSocket.send(new Gson().toJson(new SocketSendBean(Constans.NO_FIVE_PUSH)));
//            stockSocketFive.send(new Gson().toJson(new SocketSendBean(Constans.FIVE_PUSH)));
        }

    }


    public void closeWebSocket() {
        stockSocket = null;

        DisposUtil.close(stockDisposable);
    }


}
